// ignore_for_file: deprecated_member_use

import 'package:flutter/material.dart';

class NormalInput extends StatelessWidget {
  final TextEditingController controller;
  final String? title;
  final double? height;
  final double radius;
  final void Function(String)? onSubmitted;
  final bool showClear;
  final FocusNode? focusNode;
  final IconData? prefixIcon;
  final String? hintText;

  const NormalInput({
    super.key,
    required this.controller,
    this.title = '',
    this.height = 40,
    this.onSubmitted,
    this.showClear = true,
    this.radius = 20,
    this.focusNode,
    this.prefixIcon,
    this.hintText,
  });

  OutlineInputBorder _buildCustomBorder({Color? color}) {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(radius),
      borderSide: BorderSide(
        color: color ?? Colors.grey.withOpacity(0.4), // 支持自定义颜色
        width: 1,
      ),
    );
  }

  InputDecoration _buildInputDecoration(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return InputDecoration(
        hintText: hintText ?? '请输入',
        hintStyle: TextStyle(color: Colors.grey[500]),
        fillColor: theme.primaryContainer.withOpacity(0.5),
        filled: true,
        border: _buildCustomBorder(),
        enabledBorder: _buildCustomBorder(
          color: theme.onSurface.withOpacity(0.2),
        ), // 常规下的边框色
        focusedBorder: _buildCustomBorder(
          color: theme.onSurface.withOpacity(0.5),
        ), // 聚焦时的边框色
        contentPadding: EdgeInsets.symmetric(
          vertical: height! * 0.3,
          horizontal: 16,
        ),
        // 仅有内容时显示清除按钮
        suffixIcon:
            controller.text.isNotEmpty ? _buildClearButton(context) : null,
        //左侧图标
        prefixIcon: Icon(prefixIcon ?? Icons.search));
  }

  Widget _buildClearButton(BuildContext context) {
    return Material(
      color: Colors.transparent,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(radius),
      ),
      child: InkWell(
        borderRadius: BorderRadius.circular(radius),
        onTap: () {
          controller.clear();
          focusNode?.unfocus();
        },
        child: Icon(
          Icons.clear,
          size: 20,
        ),
      ),
    );
  }

  Widget _buildTextFormField(BuildContext context) {
    return TextFormField(
      controller: controller,
      focusNode: focusNode,
      style: const TextStyle(
        fontWeight: FontWeight.w400,
        height: 1,
      ),
      onFieldSubmitted: onSubmitted,
      decoration: _buildInputDecoration(context),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 0,
      color: Colors.transparent,
      borderOnForeground: true,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(radius),
      ),
      child: SizedBox(
        width: double.infinity,
        height: height,
        child: ValueListenableBuilder<TextEditingValue>(
          valueListenable: controller,
          builder: (context, _, __) => _buildTextFormField(context),
        ),
      ),
    );
  }
}
